package com.xlh.dao.conclude.helper;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.xlh.dao.conclude.BusiFullMarkMapper;
import com.xlh.pojo.conclude.BusiFullMark;
import com.xlh.pojo.conclude.example.BusiFullMarkExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author sy
 */
@Repository
public class BusiFullMarkMapperHelper {


    @Autowired
    private BusiFullMarkMapper busiFullMarkMapper;


    public BusiFullMark get(String sysFunkKey, Long courseId, Long classId, Long chapterId) {

        BusiFullMarkExample example = new BusiFullMarkExample();
        BusiFullMarkExample.Criteria criteria = example.createCriteria()
                .andBusiTypeEqualTo(Integer.valueOf(sysFunkKey))
                .andCourseIdEqualTo(courseId)
                .andClassIdEqualTo(classId)
                .andIsDeleteEqualTo(false);
        if(ObjectUtil.isNotNull(chapterId)){
            criteria.andChapterIdEqualTo(chapterId);
        }
        List<BusiFullMark> busiFullMarks = busiFullMarkMapper.selectByExample(example);

        if(CollectionUtil.isNotEmpty(busiFullMarks)){

            return busiFullMarks.get(0);
        }
        // 返回默认的满分为100分
        BusiFullMark busiFullMark = new BusiFullMark();
        busiFullMark.setFullMark(100);
        busiFullMark.setBusiType(Integer.valueOf(sysFunkKey));
        busiFullMark.setCourseId(courseId);
        busiFullMark.setClassId(classId);
        busiFullMark.setChapterId(chapterId);

        return busiFullMark;
    }


    public void update(BusiFullMark busiFullMark) {

        busiFullMark.setUpdateTime(null);
        busiFullMarkMapper.updateByPrimaryKeySelective(busiFullMark);
    }


    public void insert(BusiFullMark busiFullMark) {

        busiFullMarkMapper.insertSelective(busiFullMark);
    }
}
